#include "widget.h"
#include "ui_widget.h"
#include <QIcon>
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 创建图标
    QIcon icon(":/Sett.png");
    // 设置图标
    ui->pushButton_target->setIcon(icon);
    ui->pushButton_target->setIconSize(QSize(100,100));
    ui->pushButton_up->setIcon(QIcon(":/up.png"));
    ui->pushButton_down->setIcon(QIcon(":/down.png"));
    ui->pushButton_left->setIcon(QIcon(":/left.png"));
    ui->pushButton_right->setIcon(QIcon(":/right.png"));

    // 设置快捷键
    ui->pushButton_up->setShortcut(QKeySequence("w"));
    ui->pushButton_down->setShortcut(QKeySequence("s"));
    ui->pushButton_left->setShortcut(QKeySequence("a"));
    ui->pushButton_right->setShortcut(QKeySequence("d"));

    // 设置快捷键也可以
//    ui->pushButton_up->setShortcut(QKeySequence(Qt::Key_W));
//    ui->pushButton_down->setShortcut(QKeySequence(Qt::Key_S));
//    ui->pushButton_left->setShortcut(QKeySequence(Qt::Key_A));
//    ui->pushButton_right->setShortcut(QKeySequence(Qt::Key_D));

    ui->pushButton_up->setAutoRepeat(true);
    ui->pushButton_down->setAutoRepeat(true);
    ui->pushButton_left->setAutoRepeat(true);
    ui->pushButton_right->setAutoRepeat(true);

    ui->pushButton_up->setAutoRepeatDelay(1000);
    ui->pushButton_up->setAutoRepeatInterval(5000);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_up_clicked()
{
    const QRect& rect = this->ui->pushButton_target->geometry();
    ui->pushButton_target->setGeometry(rect.x(), rect.y() - 5,
                                       rect.width(), rect.height());
    qDebug() << "up";
}

void Widget::on_pushButton_left_clicked()
{
    const QRect& rect = this->ui->pushButton_target->geometry();
    ui->pushButton_target->setGeometry(rect.x() - 5, rect.y() ,
                                       rect.width(), rect.height());
    qDebug() << "left";
}

void Widget::on_pushButton_right_clicked()
{
    const QRect& rect = this->ui->pushButton_target->geometry();
    ui->pushButton_target->setGeometry(rect.x() + 5, rect.y() ,
                                       rect.width(), rect.height());
    qDebug() << "right";
}

void Widget::on_pushButton_down_clicked()
{
    const QRect& rect = this->ui->pushButton_target->geometry();
    ui->pushButton_target->setGeometry(rect.x(), rect.y() + 5,
                                       rect.width(), rect.height());
    qDebug() << "down";
}
